﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
public partial class MasterPages_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitForm();
        }
    }
    protected void InitForm()
    {
        username_textbox.Focus();
    }
    //protected bool RestaurantLogin(String user, String pwd)
    //{
    //    String sqlRestaurantLogin = "SELECT restaurant.name as name_restaurant,restaurant.address as address_restaurant," +
    //                                "restaurant.tel as tel_restaurant,restaurant.remark as adver_restaurant," +
    //                                "convert(float,restaurant.start_time,8) as start_time_restaurant,"+
    //                                "convert(float,restaurant.close_time,8) as close_time_restaurant," +
    //                                "is_open as is_open_restaurant,owner.name as name_owner,owner.pwd as pwd_owner," +
    //                                "identity_card as identity_card_owner,owner.phone as mobile_owner,restaurant.id as restaurant_id " +
    //                                "FROM restaurant,owner " +
    //                                "WHERE owner.name='" + user + "' and owner.pwd='" + pwd + "' and owner.id=owner_id";
    //    DataSet ds_restaurantlogin = DB.QuerySelect(sqlRestaurantLogin);
    //    if (ds_restaurantlogin == null || ds_restaurantlogin.Tables.Count != 1 || ds_restaurantlogin.Tables[0].Rows.Count != 1)
    //    {
    //        return false;
    //    }
        
    //    Session["name_restaurant"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[0];
    //    Session["address_restaurant"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[1];
    //    Session["tel_restaurant"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[2];
    //    Session["adver_restaurant"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[3];
    //    Session["start_time_restaurant"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[4];
    //    Session["close_time_restaurant"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[5];
    //    Session["is_open_restaurant"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[6].ToString();
    //    Session["username_owner"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[7];
    //    Session["pwd_owner"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[8];
    //    Session["identity_card_owner"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[9];
    //    Session["mobile_owner"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[10];
    //    Session["restaurant_id"] = ds_restaurantlogin.Tables[0].Rows[0].ItemArray[11];

    //    return true;
    //}
    protected void UpdateNotice(String notice,bool isWarning)
    {
        notice_lable.Text = notice;

        if (isWarning)
            notice_lable.ForeColor = System.Drawing.Color.Red;
    }
    protected void OnLoginBtnClick(object sender, EventArgs e)
    {
        Owner owner = new Owner();
        owner.Name = username_textbox.Text.Trim();
        owner.Pwd = pwd_textbox.Text.Trim();

        if (owner.Name.Equals("freedom") && owner.Pwd.Equals("password"))
        {
            Response.Redirect("~/admin/login.aspx");
        }

        if (!BLL.Command.OwnerLogin(ref owner))
        {
            UpdateNotice("用户名或密码错误!",true);
            return;
        }

        UpdateNotice(owner.Name + " 登录成功!", false);
        Session["owner_id"] = owner.Id;

        Response.Redirect("~/restaurant/admin.aspx?owner_id="+owner.Id);
    }
    protected void OnRegisterBtnClick(object sender, EventArgs e)
    {
        Session["back_url"] = "~/restaurant/login.aspx";

        Response.Redirect("~/restaurant/RegisterProvider.aspx");
    }
}
